package cn.edu.scau.cmi.huangzhiyuan.db.listener;

import java.util.List;

import javax.persistence.Id;

import org.jboss.jandex.Main;

import cn.edu.scau.cmi.huangzhiyuan.db.client.MainPane;
import cn.edu.scau.cmi.huangzhiyuan.db.dao.StudentDao;
import cn.edu.scau.cmi.huangzhiyuan.db.dao.TeacherDao;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Person;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Student;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Teacher;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;

/**
 * 监听类，UI组件时事件
 * @author Yuan
 *
 */
public class Listener {

	private static MainPane mainPane;
	private static StudentDao studentDao;
	private static TeacherDao teacherDao;
	private static List<Person> teachers;
	private static List<Person> students;

	public static void addListener(MainPane pane) {
		mainPane = pane;
		studentDao = new StudentDao();
		teacherDao = new TeacherDao();
		initPane();
		addStudentPaneListener();
		addTeacherPaneListener();
	}

	private static void initPane() {
		teachers = teacherDao.findAll();
		students = studentDao.findAll();
		// 初始化學生數據
		mainPane.studentPane.headerPane.setTutors(teachers);
		mainPane.studentPane.bodyPane.setPerson(students);
		// 初始化教師數據
		mainPane.teacherPane.bodyPane.setPerson(teachers);
		mainPane.teacherPane.setAppendStudentBoxList(students);
	}

	private static void addStudentPaneListener() {
		// 监听添加学生按钮
		Button addStudentBtn = mainPane.studentPane.headerPane.addBtn;
		addStudentBtn.setOnAction((event) -> {
			// 获取输入姓名
			TextField input = mainPane.studentPane.headerPane.nameInput;
			String name = input.getText();
			// 获取教师id
			int id = mainPane.studentPane.headerPane.tutors.getSelectionModel().getSelectedItem().getId();
			Teacher teacher = teacherDao.findById(id);
			// 使用DAO对象保存
			Student student = new Student();
			student.setName(name);
			student.setTeacher(teacher);
			studentDao.save(student);
			// 清空输入框
			input.clear();
			// 更新数据
			initPane();
		});

		// 监听学生列表选择
		ChoiceBox<Person> names = mainPane.studentPane.bodyPane.names;
		names.setOnAction((event) -> {
			if(names != null) {
				// Person转换成Student
				int id = names.getSelectionModel().getSelectedItem().getId();
				Student student = studentDao.findById(id);
				// 更新展示列表
				mainPane.studentPane.setFooter(student, teachers);
				// 设置学生详细面板为可见
				mainPane.studentPane.setFooterVisible(true);
			}
		});
		
		// 监听删除学生按钮
		Button delBtn = mainPane.studentPane.getFooterPaneDelBtn();
		delBtn.setOnAction((event) -> {
			if(mainPane.studentPane.bodyPane.names!=null) {
				// 获取学生对象
				Person person = mainPane.studentPane.bodyPane.names.getSelectionModel().getSelectedItem();
				Student student = studentDao.findById(person.getId());
				// 调用dao层删除
				studentDao.delete(student);
				// 更新列表
				initPane();
				// 隐藏学生信息展示
				mainPane.studentPane.setFooterVisible(false);
			}
		});
		
		// 监听保存学生修改按钮
		Button saveBtn = mainPane.studentPane.getFooterPaneSaveBtn();
		saveBtn.setOnAction((event) -> {
			// 获取id
			int id = Integer.valueOf(mainPane.studentPane.getFooterPaneIdInput().getText());
			// 获取姓名
			String name = mainPane.studentPane.getFooterPaneNameInput().getText();
			// 获取教师
			Person person = mainPane.studentPane.getFooterPaneTutors().getSelectionModel().getSelectedItem();
			Teacher teacher = teacherDao.findById(person.getId());
			// 调用dao层进行更新
			Student student = new Student();
			student.setId(id);
			student.setName(name);
			student.setTeacher(teacher);
			studentDao.update(student);
			// 更新列表
			initPane();
		});
	}
	
	private static void addTeacherPaneListener() {
		// 监听添加教师按钮
		Button addBtn = mainPane.teacherPane.headerPane.addBtn;
		addBtn.setOnAction((event) -> {
			// 获取输入姓名
			TextField nameInput = mainPane.teacherPane.headerPane.nameInput;
			// 调用dao层保存
			Teacher teacher = new Teacher();
			teacher.setName(nameInput.getText());
			teacherDao.save(teacher);
			// 清除输入框
			nameInput.clear();
			// 更新列表
			initPane();
		});
		
		// 监听教师选择列表
		ChoiceBox<Person> names = mainPane.teacherPane.bodyPane.names;
		names.setOnAction((event) -> {
			/* 
			 * 没有任何数据时，namesnull
			 * bodyPane重置students时，也会触发列表监听器
			 * 所以要判断names和getselectitem的情况
			 */
			if(names != null && names.getSelectionModel().getSelectedItem()!=null) {
				// Person转换成Teacher
				int id = names.getSelectionModel().getSelectedItem().getId();
				Teacher teacher = teacherDao.findById(id);
				// 更新展示列表
				mainPane.teacherPane.setFooterPane(teacher, students);
			}
		});
		
		// 监听选择学生按钮
		Button selectBtn = mainPane.teacherPane.getAppendBoxSelectBtn();
		selectBtn.setOnAction((event) -> {
			// 找出被选中学生
			ChoiceBox<Person> cb = mainPane.teacherPane.getAppendBoxChoiceBox();
			Person person = cb.getSelectionModel().getSelectedItem();
			Student student = studentDao.findById(person.getId());
			// 找出当前老师
			person = mainPane.teacherPane.bodyPane.names.getSelectionModel().getSelectedItem();
			Teacher teacher = teacherDao.findById(person.getId());
			// 调用dao层添加
			teacher.getStudents().add(student);
			teacherDao.update(teacher);
			// 更新列表
			initPane();
		});
		
		// 监听教师修改保存按钮
		Button saveBtn = mainPane.teacherPane.getTeacherInfoSaveBtn();
		saveBtn.setOnAction((event) -> {
			// 获取教师姓名
			String name = mainPane.teacherPane.getTeacherInfoNameInput().getText();
			int id = Integer.valueOf(mainPane.teacherPane.getTeacherInfoIdInput().getText());
			// 调用dao层保存
			Teacher teacher = teacherDao.findById(id);
			teacher.setName(name);
			teacherDao.update(teacher);
			// 更新列表
			initPane();
		});
		
		// 监听教师删除按钮
		Button delBtn = mainPane.teacherPane.getTeacherInfoDelBtn();
		delBtn.setOnAction((event) -> {
			// 获取教师对象
			int id = Integer.valueOf(mainPane.teacherPane.getTeacherInfoIdInput().getText());
			Teacher teacher = teacherDao.findById(id);
			// 调用dao删除
			teacherDao.delete(teacher);
			// 更新列表
			initPane();
			// 清空输入框
			mainPane.teacherPane.clearTeacherInfoAttributes();
		});
		
		// 监听删除指导学生按钮
		// StudentPane内部类实现
	}

}
